package by.bsuir.util;
import by.bsuir.entity.Node;
import jcifs.smb.*;

/**
 * @author Raman_Pliashkou
 */
public class AuthListFiles extends NtlmAuthenticator {

    private String login;

    private String password;

    private String domain;

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public static String readLine() throws Exception {
        int c;
        StringBuffer sb = new StringBuffer();
        while ((c = System.in.read()) != '\n') {
            if (c == -1)
                return "";
            sb.append((char) c);
        }
        return sb.toString().trim();
    }

    public void getTree(Node node) throws Exception {

        String link = null;

        if (node.isTypeShare()) {
            link = "smb://" + node.getHost() + "/" + node.getName() + "/";
        } else {
            link = "smb://" + node.getHost() + "/" + node.getName();
        }
        //System.out.println(link);
        SmbFile file = new SmbFile(link);
        SmbFile[] files = file.listFiles();

        for (int i = 0; i < files.length; i++) {
            Node nodeChild = new Node();
            nodeChild.setName(files[i].getName());
            nodeChild.setHost(node.getHost());
            nodeChild.setTypeShare(false);
            node.getNodes().add(nodeChild);
            //System.out.println(" " + files[i].getName());
            if (files[i].isDirectory()) {
                nodeChild.setType(true);
                if (node.isFolder() && !node.isTypeShare()) {
                    nodeChild.setName(node.getName() + nodeChild.getName());
                } else {
                    nodeChild.setName(node.getName() + "/" + nodeChild.getName());
                }
                getTree(nodeChild);
            } else
                nodeChild.setType(false);
        }
        //System.out.println();
    }

    // public void getTree(String name) throws Exception {
    //
    // SmbFile file = new SmbFile(name);
    // SmbFile[] files = file.listFiles();
    //
    // for (int i = 0; i < files.length; i++) {
    // System.out.println(" " + files[i].getName());
    // if (files[i].isDirectory()) {
    // getTree(name + files[i].getName());
    // }
    // }
    // System.out.println();
    // }

    public AuthListFiles(String login, String password, String domain) throws Exception {
        this.domain = domain;
        this.login = login;
        this.password = password;
        NtlmAuthenticator.setDefault(this);
    }

    protected NtlmPasswordAuthentication getNtlmPasswordAuthentication() {
        // System.out.println( getRequestingException().getMessage() + " for " + getRequestingURL() );
        try {
            if (password.length() == 0) {
                return null;
            }
            return new NtlmPasswordAuthentication(domain, login, password);
        } catch (Exception e) {
        }
        return null;
    }
}
